package com.api.sw.duban.web;

import com.alibaba.fastjson.JSONObject;
import com.api.sw.duban.action.DubanJcAction;
import com.api.sw.duban.action.DubanJcImportAction;
import com.api.sw.duban.action.DubanqsAction;
import com.api.sw.duban.action.DubanyYbAction;
import com.api.sw.duban.model.Uf_jcdcrw;
import com.api.sw.duban.util.CacheUtil;
import com.api.sw.duban.util.DubUtil;
import com.api.sw.duban.util.LogTool;
import com.api.sw.duban.util.UploadWord;
import com.engine.common.util.ParamUtil;
import weaver.conn.RecordSet;
import weaver.file.FileUpload;
import weaver.file.FileUploadToPath;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.workflow.workflow.WorkflowComInfo;
import weaver.workflow.workflow.WorkflowVersion;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
    决策督查
 */
@Path("/dubanjclx")
public class DbanJcAction {
    private final static LogTool log = new LogTool("/log/duban/",false);

    //获取查询列表数据页面
    @GET
    @Path("/tablelist")
    @Produces({"text/plain"})
    public String getJctablelist(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        System.out.println("de");
        try {
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);
            DubanJcAction DubanjcAction=new DubanJcAction();
            apidatas.putAll(DubanjcAction.getTablelist(ParamUtil.request2Map(request),user));
            apidatas.put("api_status", true);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }
    /*
     决策督察 台账列表
     */
    @GET
    @Path("/jctablelist")
    @Produces({"text/plain"})
    public String getJcTtablelist(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        System.out.println("de");
        try {
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);
            DubanJcAction DubanjcAction=new DubanJcAction();
            apidatas.putAll(DubanjcAction.getTablelistZt(ParamUtil.request2Map(request),user));
            apidatas.put("api_status", true);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }
    //获取查询列表数据页面
    @GET
    @Path("/condition")
    @Produces({"text/plain"})
    public String getQscondition(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        System.out.println("de");
        try {
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);
            DubanJcAction DubanjcAction=new DubanJcAction();
            apidatas.putAll(DubanjcAction.getCondition(ParamUtil.request2Map(request),user));
            apidatas.put("api_status", true);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }
    /*
        决策督察台账
     */
    @GET
    @Path("/jccondition")
    @Produces({"text/plain"})
    public String getJccondition(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();

        try {
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);
            DubanJcAction DubanjcAction=new DubanJcAction();
            apidatas.putAll(DubanjcAction. getConditionTz(ParamUtil.request2Map(request),user));
            apidatas.put("api_status", true);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }
    //检测是否可以编辑 删除 交办等操作
    @GET
    @Path("/checkButton")
    @Produces({"text/plain"})
    public String checkButton(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        JSONObject json=new JSONObject();
        System.out.println("de");
        try {
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);
            DubanqsAction DubanqsAction =new DubanqsAction();
            json=DubanqsAction.checkButton(ParamUtil.request2Map(request),user);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(json);
    }
    //获取收发文部门对应的督查人员
    @GET
    @Path("/getDcryByReceiveunit")
    @Produces({"text/plain"})
    public String getDcryByReceiveunit(@Context HttpServletRequest request, @Context HttpServletResponse response){
        JSONObject json=new JSONObject();
        String Receiveunit=Util.null2String(request.getParameter("Receiveunitid"));
        String receiveunitry= Util.null2String(DubUtil.getDcryByReceiveunit(Receiveunit));
        json.put("receiveunitry",DubUtil.getHrmspecialobj(receiveunitry));
        return JSONObject.toJSONString(json);
    }
    @GET
    @Path("/getSubmitCondition")
    @Produces({"text/plain"})
    public String getSubmitCondition(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        System.out.println("de");
        try {
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);
            DubanJcAction DubanjcAction=new DubanJcAction();
            apidatas.putAll(DubanjcAction.getSubmitCondition(ParamUtil.request2Map(request),user));
            apidatas.put("api_status", true);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }

    @POST
    @Path("/import")
    @Produces({"text/plain"})
    public String Setimport(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        System.out.println("de");
        String mess="";
        boolean api_status=true;
        try {
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);
            //获取流数据
            String tzid=Util.null2String(request.getParameter("tzid"));//在通知ID
            log.writeLog("Setimport--"+tzid);
            FileUploadToPath fu = new FileUploadToPath(request) ; //上传附件到临时目录
            String filePath = fu.uploadFiles1("file");
            //log.writeLog(filePath);
            FileInputStream FileInputStream= new FileInputStream(filePath);

            List<Uf_jcdcrw> Uf_jcdcrw=new ArrayList<Uf_jcdcrw>();
            UploadWord UploadWord=new UploadWord();
            //FileInputStream in, List<Uf_jcdcrw> Uf_jcdcrw,String tzid
            mess=UploadWord.UploadWord3(FileInputStream,Uf_jcdcrw,tzid);
            log.writeLog(mess);
            if("".equals(mess)){
                mess=UploadWord.CreateWorflow(user,Uf_jcdcrw);
                if(!"".equals(mess)){
                    api_status=false;
                }

            }else{
                api_status=false;
            }
            apidatas.put("status", api_status);
        } catch (Exception e) {
            mess="异常"+e.getMessage();
            e.printStackTrace();
            apidatas.put("status", false);
            apidatas.put("msg", "catch exception : " + e.getMessage());
        }
        if(!"".equals(mess)){
            apidatas.put("msg",mess);
        }else{
            JSONObject data=new JSONObject();
            data.put("fileid","123");
            apidatas.clear();
            apidatas.put("data",data);
            apidatas.put("status","1");
        }

        return JSONObject.toJSONString(apidatas);
    }
    @POST
    @Path("/loadbutton")
    @Produces({"text/plain"})
    public String getButtonQx(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        JSONObject JSON=new JSONObject();
        System.out.println("de");
        try {
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);
            String tzid=Util.null2String(request.getParameter("tzid"));
            RecordSet re=new RecordSet();
            re.execute("select * from uf_jcgzsd where ssdctz='"+tzid+"'");
            if(re.next()){
                String id=re.getString("id");
                JSON.put("gzid",id);
            }
            apidatas.put("api_status", true);
            apidatas.put("data", JSON);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }
    /*
        获取通知对应的任务
     */
    @POST
    @Path("/gettzrw")
    @Produces({"text/plain"})
    public String gettzrw(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        System.out.println("de");
        try {
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);
            DubanJcAction DubanjcAction=new DubanJcAction();
            apidatas.putAll(DubanjcAction.gettzrw(ParamUtil.request2Map(request),user));
            apidatas.put("api_status", true);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }
    @POST
    @Path("/getrwCondition")
    @Produces({"text/plain"})
    public String getrwCondition(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        System.out.println("de");
        try {
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);
            DubanJcAction DubanjcAction=new DubanJcAction();
            apidatas.putAll(DubanjcAction.getrwCondition(ParamUtil.request2Map(request),user));
            apidatas.put("api_status", true);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }
    @GET
    @Path("/jcimport")
    @Produces({"text/plain"})
    public String JcImport(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        System.out.println("de");
        try {
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);
            DubanJcImportAction DubanJcImportAction=new DubanJcImportAction();
            apidatas.putAll(DubanJcImportAction.getTablelist(ParamUtil.request2Map(request),user));
            apidatas.put("flag", true);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("flah", false);
            apidatas.put("flag", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }
    /*
        保存标签页
     */
    @POST
    @Path("/savebqk")
    @Produces({"text/plain"})
    public String saveBqk(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        System.out.println("de");
        try {
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);
            DubanJcAction DbanJcAction=new DubanJcAction();
            apidatas=DbanJcAction.saveBqk(ParamUtil.request2Map(request),user);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("flah", false);
            apidatas.put("flag", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }
    //为任务设置标签
    @POST
    @Path("/saverwbqk")
    @Produces({"text/plain"})
    public String saveRwBqk(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        System.out.println("de");
        try {
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);
            DubanJcAction DbanJcAction=new DubanJcAction();
            apidatas=DbanJcAction.saveRwBqk(ParamUtil.request2Map(request),user);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("flah", false);
            apidatas.put("flag", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }
    //清楚任务
    @POST
    @Path("/clerrwbqk")
    @Produces({"text/plain"})
    public String clerRwbqk(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        System.out.println("de");
        try {
            //获取当前用户

            User user = HrmUserVarify.getUser(request, response);
            DubanJcAction DbanJcAction=new DubanJcAction();
            apidatas=DbanJcAction.clerRwBqk(ParamUtil.request2Map(request),user);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("flah", false);
            apidatas.put("flag", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }


}
